﻿#include<stdio.h>
#include<stdlib.h>
#include"state.h"

int main(){
	HuffmanTree ht;
	char **hucode;
	/*
	 * code为返回的100*m的一维数组
	 * charset为字符集
	 * fincode为二维数组
	 * 保存了句子与编码的对应关系
	 */
	int n, i, m, *code, j, *k;
	char *charset,fincode[100][100], *ccode;
	printf("Enter the size of charset(n): ");
	scanf("%d",&n);
	charset = (char *)malloc(n * sizeof(char));
	printf("Enter the length of encoding(m): ");
	scanf("%d", &m);
	printf("Enter the %d chars in the charset: ", n);
	getchar();
	for(i = 0; i < n; i++)
		scanf("%c", &charset[i]);
	ccode = (char *)malloc(100 * m * sizeof(char));
	code = (int *)malloc(100 * m * sizeof(int));
	huffCode(code, n, m);
	printf("The 100 sentences encoding like these: \n");
	for(i = 0; i < 100; i++)
		for(j = 0; j < m; j++)
			fincode[i][j]=charset[code[i*m+j]];
	for(i = 1; i <= 100; i++){
		printf("%2d: ", i);
		for(j = 0; j < m; j++){
			printf("%c",fincode[i-1][j]);
			ccode[(i-1)*m+j]=fincode[i-1][j];
		}printf("    ");
		if(i%4 == 0)	printf("\n");
	}
	printf("\n");
	k = count(ccode, m*100, n, charset); 	
	hucode=HuffmanCoding(&ht, k, n);
	printf("******************************************\n"
		"* Char\tOccurrenceTimes  \tHuffCodes\n");
	for(i=0;i<n;i++)
		printf("* %c\t%d\t\t\t%s\n", charset[i], k[i], hucode[i]);
	printf("******************************************\n");
}
